// 引入此文件需自建如下内容：
// footerButtons
import moment from 'moment';
import { transferDataDetail } from './utils';
import { FORECAST_TYPE as PAGE_TYPE } from '@/constants/index';
import { mapGetters } from 'vuex';
let index = 0;
export default {
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    detail: {
      type: Object,
      default: () => ({}),
    },
    planId: {
      type: [String, Number],
      default: '',
    },
    pageType: {
      type: String,
      default: 'M9',
    },
    currentNode: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    this.PAGE_TYPE = PAGE_TYPE;
    return {
      form: {},
      planDetailList: [],
      endPlanDetailList: [],
      thirdDetailList: [],
      fourthDetailList: [],
      totalDetailList: [],
    };
  },
  computed: {
    ...mapGetters(['spinning']),

    timeRange() {
      return [
        moment(this.form.startTime).format('YYYY-MM-01 00:00:00'),
        moment(this.form.endTime).format('YYYY-MM-01 00:00:00'),
      ];
    },

    titleName() {
      const type = `${PAGE_TYPE[this.pageType]}预测`;
      const node = this.currentNode.label;
      const status =
        this.hasEdit && this.currentNode.key !== 'Start' ? '待办' : undefined;
      return [type, node, status].filter(item => item).join('-');
    },
    processId() {
      return this.detail?.processId;
    },
    hasEdit() {
      return this.detail.isCurrentProcessor === false ? false : true;
    },
    filterButtons() {
      if (this.hasEdit) return this.footerButtons;
      return this.footerButtons.filter(item => !item.auth);
    },

    logList() {
      return this.detail?.logList || [];
    },
  },
  watch: {
    visible: {
      handler(val) {
        if (!val) return;
        const plan = this.detail || {};
        const timeRanges = [plan.startTime, plan.endTime];
        this.form = {
          salesOrganization: plan.salesOrganization || '',
          serialNumber: plan.serialNumber || undefined,
          name: plan.name || '',
          currStatus: plan.currStatus,
          startTime: plan.startTime && moment(plan.startTime),
          endTime: plan.endTime && moment(plan.endTime),
          deadline: plan.deadline || null,
          responsibleId: {
            key: plan.responsibleId,
            label: plan.responsibleName || '',
          },
          operatingOrganization: plan.operatingOrganization,
        };

        // 初版明细
        const planDetailList = this.detail?.fcstDetailList || [];
        this.planDetailList = transferDataDetail(planDetailList, timeRanges);

        // 终版明细
        const endPlanDetailList = this.detail?.doForecastList || [];
        if (endPlanDetailList.length) {
          this.endPlanDetailList = transferDataDetail(
            endPlanDetailList,
            timeRanges
          );
        }

        const thirdDetailList = this.detail?.supplyPlanList || [];

        if (thirdDetailList.length) {
          this.thirdDetailList = transferDataDetail(
            thirdDetailList,
            timeRanges
          );
        }

        const fourthDetailList = this.detail?.salesForecastList || [];
        console.log('fourthDetailList: ', fourthDetailList);

        if (fourthDetailList.length) {
          this.fourthDetailList = transferDataDetail(
            fourthDetailList,
            timeRanges
          );
        }

        const totalDetailList = this.detail?.mergeList || [];
        if (totalDetailList.length) {
          this.totalDetailList = totalDetailList.map(item => {
            return {
              ...item,
              id: item.id ? item.id : `random_${++index}`,
              customerCode: {
                key: item.customerCode,
                label: item.customerName,
              },
              modelName: {
                key: item.modelCode,
                label: item.modelName,
              },
              modelNumber: {
                key: item.modelNumber,
                label: item.modelInfoName,
              },
              fcst: JSON.parse(item.fcst || '{}'),
              lastFcst: JSON.parse(item.lastFcst || '{}'),
              price: JSON.parse(item.price || '{}'),
              executeForecast: JSON.parse(item.executeForecast || '{}'),
              salesForecast: JSON.parse(item.salesForecast || '{}'),
              supplyPlan: JSON.parse(item.supplyPlan || '{}'),
            };
          });
        }
      },
      immediate: true,
    },
  },
  methods: {},
};
